package com.withoutwords.engine.social;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

import com.withoutwords.engine.ConvertableJson;

public class SocialUser implements ConvertableJson, Serializable,Comparable<SocialUser> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5823844753601297309L;

	String SocialID = "";

	public String getSocialID() {
		return SocialID;
	}

	public void setSocialID(String socialID) {
		SocialID = socialID;
	}

	String Name;

	@Override
	public String toString() {
		return Name;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	SocialNetwork network;

	@Override
	public JSONObject toJSON() {
		JSONObject obj = new JSONObject();
		try {
			if (Name != null)
				obj.put("name", Name);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (SocialID != null)
			obj.put("social_id", SocialID);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (network != null)
				obj.put("social_network", network.ordinal());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return obj;
	}

	@Override
	public boolean fromJSON(JSONObject obj) {

		try {
			Name = obj.getString("name");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// return false;
		}

		try {

			SocialID = obj.getString("social_id");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// return false;
		}

		try {
			network = SocialNetwork.values()[obj.getInt("social_network")];

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// return false;
		}
		return true;

	}

	@Override
	public int compareTo(SocialUser another) {
		
		return this.Name.compareTo(another.Name);
	}

	@Override
	public boolean equals(Object o) {
		SocialUser other = (SocialUser)o;
		// TODO Auto-generated method stub
		return this.SocialID.equals(other.SocialID);
	}

	}
